package me.jessyan.mvparms.demo.mvp.ui.activity.car;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.example.xrecyclerview.XRecyclerView;
import com.example.xrecyclerview.refresh.SwipyRefreshLayout;
import com.example.xrecyclerview.refresh.SwipyRefreshLayoutDirection;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.car.DaggerChoiceCarComponent;
import me.jessyan.mvparms.demo.di.module.car.ChoiceCarModule;
import me.jessyan.mvparms.demo.mvp.contract.car.ChoiceCarContract;
import me.jessyan.mvparms.demo.mvp.presenter.car.ChoiceCarPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.ChoiceCarAdapter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.mvp.ui.widget.PercentLinearLayout;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 选择车辆
 */
public class ChoiceCarActivity extends BaseActivity<ChoiceCarPresenter> implements ChoiceCarContract.View {

    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.record_tv)
    TextView recordTv;
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;
    @BindView(R.id.refresh)
    SwipyRefreshLayout refreshLayout;

    @BindView(R.id.empty_ll)
    PercentLinearLayout emptyLl;


    ChoiceCarAdapter adapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {


        DaggerChoiceCarComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .choiceCarModule(new ChoiceCarModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_choice_car; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {


        initRecycler();

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });
    }

    private void initRecycler() {

        List<String> list = new ArrayList<>();

        adapter = new ChoiceCarAdapter(list);
        refreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                list.add("s");
                list.add("s");
                list.add("s");
                adapter.notifyDataSetChanged();
            }
        });
        LinearLayoutManager lr = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);

        recyclerView.setLayoutManager(lr);
        recyclerView.setAdapter(adapter);
        ArmsUtils.configRecyclerView(recyclerView, lr);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();
        recyclerView.setLoadingMoreEnabled(false);

        recyclerView.setEmptyView(emptyLl);

        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                        startActivity(new Intent(ChoiceCarActivity.this,PayCostActivity.class));
            }
        });

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.record_tv, R.id.notice_bind_tv})
    public void onViewClicked(View view) {
        if (DoubleClickUtils.getInstance().isDouble(view)) {
            return;
        }
        switch (view.getId()) {
            case R.id.record_tv:
                startActivity(new Intent(ChoiceCarActivity.this, PayCostRcordActivity.class));
                break;
            case R.id.notice_bind_tv:
                startActivity(new Intent(ChoiceCarActivity.this,MyCarsActivity.class));
                break;
            default:

        }
    }
}
